package com.weipai.springboot.module.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonUtil {
    /***
     * 将JavaBean 序列化成JSON格式的字符串
     * <br>该方法采用fastjson处理
     * @param obj
     * @return JSON格式的字符串
     */
    public static String toJSON(Object obj){
        if(obj==null){
            return null ;
        }
        return  JSON.toJSONString(obj);
    }

    /**
     * 将json 格式的字符串反序列化成对象
     * @param jsonString
     * @param clazz 对象的class
     * @return T
     */
    public static <T> T parseObject(String jsonString,Class<T> clazz){
        return JSON.parseObject(jsonString, clazz);
    }

    @SuppressWarnings("unchecked")
    public static <T> T parseObject(String jsonString,TypeReference<T> type){
        return (T) JSON.parseObject(jsonString, type);
    }

    public static JSONObject parse(String text){
        return JSON.parseObject(text);
    }


    @SuppressWarnings("rawtypes")
    public static class TypeReference<T> extends com.alibaba.fastjson.TypeReference{

        public TypeReference() {
            super();
            // TODO Auto-generated constructor stub
        }

        public TypeReference(Type... arg0) {
            super(arg0);
            // TODO Auto-generated constructor stub
        }

    }
    

    /**
     * 字符串数组转换成json 格式字符串
     * @param list
     * @return
     */
    public static String toJsonString(List<String> list){
        if(list!=null && list.size()>0){
            String json = JSONArray.fromObject(list).toString();
            return json;
        }else{
            return null;
        }
    }

    /**
     * 将图片集合存为Map
     * @param imgList
     * @return
     */
    public static List<Map<String,Object>> toMaoInList(List<String> imgList){

        List<Map<String,Object>> imageList = new ArrayList<>();
        if(imgList!=null && imgList.size()>0){
            for (String s:imgList) {
                Map<String,Object> map = new HashMap<>();
                map.put("img",s);
                imageList.add(map);
            }
        }
        return imageList;
    }

    /**
     * json 格式的字符串转换成list 数组
     * @param jsonString
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List<String> toList(String jsonString){
        if (!"".equals(jsonString) && jsonString != null) {
            JSONArray jsonArray = JSONArray.fromObject(jsonString);
            List<String> list = (List<String>) JSONArray.toCollection(jsonArray);
            return list;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public static Map<String, Object> toMap(String jsonString){
        if(!"".equals(jsonString) && jsonString!=null){
            JSONObject jasonObject = JSONObject.parseObject(jsonString);
            Map<String, Object> map = (Map<String, Object>)jasonObject;
            return map;
        }else{
            return null;
        }
    }

    
}

